package com.ty.zls.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.qmuiteam.qmui.widget.QMUITabSegment;
import com.ty.zls.MainActivity;
import com.ty.zls.R;
import com.ty.zls.adapter.ArreageAdapter;
import com.ty.zls.components.MyReceycleView;
import com.ty.zls.listener.ArreageItemListener;
import com.ty.zls.listener.ArreageLoadMoreListener;
import com.ty.zls.listener.ArreageRefreshListener;


public class ArreageFragment extends RelativeLayout {

    private Context context;
    private  QMUITabSegment.Tab arreage;
    private boolean initFlag = false;
    ArreageRefreshListener arreageRefreshListener;


    public ArreageFragment(Context context, QMUITabSegment.Tab arreage){
        super(context);
        this.context = context;
        this.arreage = arreage;
        initView();
    }

    public void initView() {
        View view =  LayoutInflater.from(context).inflate(R.layout.arreage_fragment, this);
        final SwipeToLoadLayout swipeToLoadLayout = view.findViewById(R.id.swipeToLoadLayout);
        MyReceycleView listView = view.findViewById(R.id.swipe_target);
        listView.setEmptyView(view.findViewById(R.id.ev_arreage));
        listView.setLayout(swipeToLoadLayout);
        listView.setLayoutManager(new LinearLayoutManager(context));
        final ArreageAdapter adapter = new ArreageAdapter(context);
        listView.setAdapter(adapter);
        arreageRefreshListener = new ArreageRefreshListener(adapter, context, swipeToLoadLayout, (QMUIEmptyView) listView.getmEmptyView());
        swipeToLoadLayout.setOnRefreshListener(arreageRefreshListener);
        swipeToLoadLayout.setOnLoadMoreListener(new ArreageLoadMoreListener(adapter, context, swipeToLoadLayout, (QMUIEmptyView) listView.getmEmptyView()));
        adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                arreage.showSignCountView(context, adapter.getTotal());
            }
        });
        adapter.setOnItemClickListener(new ArreageItemListener(context, adapter));
    }

    public void init(){
        if(!initFlag){
            arreageRefreshListener.autoRefresh();
            initFlag = true;
        }
    }

}
